﻿using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc.Filters;
using Nasty.Core.LoadParams;
using Quartz;

namespace Nasty.Core.Registry
{
    /// <summary>
    /// Job自动注册接口
    /// </summary>
    public interface IAutofacJob : IJob
    {
        ResultData<string> Execute(object task, IJobExecutionContext context);
    }

    public abstract class AutofacJob : IAutofacJob
    {
        /// <summary>
        /// 使用这个函数进行Job执行
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public abstract ResultData<string> Execute(object task, IJobExecutionContext context);


        /// <summary>
        /// 不要使用这个进行Job，框架要用
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                var task = context.JobDetail.JobDataMap.Get("Task");
                var result = this.Execute(task, context);
                context.JobDetail.JobDataMap.Put("Result", result);
                context.Result = result;
            }
            catch (Exception ex)
            {
                var result = new ResultData<string>() { IsSuccess = false, Message = ex.Message };
                context.JobDetail.JobDataMap.Put("Result", result);
                context.Result = result;
            }

            return Task.CompletedTask;
        }


    }
}
